package com.langhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.langhua.common.R;
import com.langhua.entity.ShoppingCart;
import com.langhua.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCarts")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据:{}",shoppingCart);

        //设置用户id，指定当前是哪个用户的购物车数据
//        Long UserId = shoppingCart.getUserId();
//        shoppingCart.setUserId(UserId);

//        Long dishId = shoppingCart.getDishId();


        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());

        //查询当前菜品是否在当前用的购物车中
        //SQL:select * from shopping_cart where user_id = ? and dish_id = ?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if(cartServiceOne != null){
            //如果已经存在，就在原来数量基础上加
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + shoppingCart.getNumber());
            cartServiceOne.setRemark(shoppingCart.getRemark());
            shoppingCartService.updateById(cartServiceOne);
        }else{
            //如果不存在，则添加到购物车
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return R.success(cartServiceOne,"添加成功");
    }

    /**
     * 查看购物车(分页)
     * @return
     */
    @GetMapping("{current}/{size}/{id}")
    public R<IPage> getPage(@PathVariable Integer current, @PathVariable Integer size,@PathVariable Long id){
        log.info("分页查看当前用户所有购物车...id为："+id);

        IPage page=new Page(current,size);
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,id);

        return R.success(shoppingCartService.page(page,lqw),"购物车分页查询");

    }
    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/getAll/{id}")
    public R<List<ShoppingCart>> getAll(@PathVariable Long id){
        log.info("查看当前用户所有购物车...id为："+id);

        LambdaQueryWrapper<ShoppingCart> lqw= new LambdaQueryWrapper();
        lqw.eq(ShoppingCart::getUserId,id);

        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list,"购物车查询所有");
    }

    @PutMapping
    public R<String> updateNumAndRemark(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车修改的数据内容为..."+shoppingCart);

        shoppingCartService.updateById(shoppingCart);

        return R.success(null,"菜单修改成功！");
    }
    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/{userId}/{dishId}")
    public R<String> deleteByIds(@PathVariable Long userId,@PathVariable Long dishId){
        //SQL:delete from shopping_cart where user_id = ? and dish_id = ?

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        lqw.eq(ShoppingCart::getDishId,dishId);

        shoppingCartService.remove(lqw);

        return R.success(null,"删除成功！");
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean/{userId}")
    public R<String> clean(@PathVariable Long userId){
        //SQL:delete from shopping_cart where user_id = ?
        log.info("清空购物车：{} userId",userId);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        shoppingCartService.remove(queryWrapper);

        return R.success("清空购物车成功");
    }

}
